package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderStatusConstant;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderTypeConstant;
import com.csun.cmny.provider.model.domain.OmcConsult;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceOrder;
import com.csun.cmny.provider.model.domain.OmcSpecialist;
import com.csun.cmny.provider.model.dto.consult.ConsultDisposeDto;
import com.csun.cmny.provider.model.dto.consult.ConsultSaveDto;
import com.csun.cmny.provider.model.dto.consult.ConsultSelectDto;
import com.csun.cmny.provider.model.vo.consult.ConsultVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcConsultService;
import com.csun.cmny.provider.service.OmcLongRangeServiceOrderService;
import com.csun.cmny.provider.service.OmcSpecialistService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.RedisUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/consult", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcConsultController extends BaseController {

    @Resource
    private OmcConsultService consultService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcSpecialistService specialistService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;


    //患者新增咨询
    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addConssult(@RequestBody @Valid ConsultSaveDto consultSaveDto, BindingResult br){



        int result = consultService.saveconsult(consultSaveDto,getLoginAuthDto());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    //专家查看咨询
    @RequestMapping(value = "/selectBySpecialist", method = RequestMethod.POST)
    public Wrapper selectBySpecialist(@RequestBody ConsultSelectDto consultSelectDto){

        //判定本人是否是专家
        OmcSpecialist omcSpecialist = new OmcSpecialist();
        omcSpecialist.setStaffUserId(getLoginAuthDto().getUserId());
        OmcSpecialist specialist = specialistService.selectOne(omcSpecialist);


        if (PublicUtil.isEmpty(specialist)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("consult.specialist.my.error", null));
        }

        consultSelectDto.setSpecialistId(specialist.getId());
        List<ConsultVo> list=consultService.selectConsult(consultSelectDto);

        PageInfo<ConsultVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    //用户查看自己的咨询
    @RequestMapping(value = "/selectByUser", method = RequestMethod.POST)
    public Wrapper selectByUser(@RequestBody  ConsultSelectDto consultSelectDto){

       // consultSelectDto.setUserId(getLoginAuthDto().getUserId());



        List<ConsultVo> list=consultService.selectConsult(consultSelectDto);
        PageInfo<ConsultVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


    //专家处理咨询
    @ValidateAnnotation
    @RequestMapping(value = "/dispose", method = RequestMethod.POST)
    public Wrapper dispose(@RequestBody  @Valid ConsultDisposeDto consultDisposeDto, BindingResult br){

        //校验此咨询是否存在
        OmcConsult omcConsult = consultService.selectByKey(consultDisposeDto.getId());
        if (PublicUtil.isEmpty(omcConsult)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("consult.isnull", null));
        }
        //校验订单是否存在
        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(omcConsult.getLongRangeServiceOrderId());
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("consult.longRangeServiceOrder.null", null));
        }
        //校验是否为咨询订单
        if (omcLongRangeServiceOrder.getOrderType()!= LongRangeServiceOrderTypeConstant.CONSULT){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("consult.longRangeServiceOrder.type.consult.error", null));
        }
        //校验是否为待回复咨询单
        if (omcLongRangeServiceOrder.getOrderStatus()!=LongRangeServiceOrderStatusConstant.WAITFORREPLY){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("consult.longRangeServiceOrder.status.consult.error", null));
        }


        int result = consultService.dispose(consultDisposeDto,omcLongRangeServiceOrder);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
